package com.hjs.servlet.dailyinspection.dailyinspectiondamagenotice;

import com.hjs.bean.DailyInspectionDamageNoticeView;
import com.hjs.mapper.ViewMapper;
import com.hjs.tools.Mapper;
import com.hjs.tools.RequestUtil;
import lombok.extern.slf4j.Slf4j;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Slf4j
@WebServlet("/dailyInspectionDamageNotice/search")
public class SearchDailyInspectionDamageNotice extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) {
        RequestUtil requestUtil = new RequestUtil(request, response);
        Mapper<ViewMapper> viewMapperMapper = new Mapper<>(ViewMapper.class);
        try {
            ViewMapper mapper = viewMapperMapper.getMapper();
            if (requestUtil.getString("id") != null) {
                DailyInspectionDamageNoticeView dailyInspectionDamageNoticeViewBy = mapper.getOneDailyInspectionDamageNoticeViewById(requestUtil.getInt("id"));
                requestUtil.responseWrite(Mapper.toJsonString(dailyInspectionDamageNoticeViewBy));
            } else if (requestUtil.getString("departmentId") != null) {
                if (requestUtil.getString("renovateEnroll") != null) {
                    List<DailyInspectionDamageNoticeView> allDailyInspectionDamageNoticeView =
                            mapper.getAllPassDailyInspectionDamageNoticeByDepartmentId(requestUtil.getInt("departmentId"));
                    requestUtil.responseWrite(Mapper.toJsonString(allDailyInspectionDamageNoticeView));
                } else {
                    List<DailyInspectionDamageNoticeView> allDailyInspectionDamageNoticeView =
                            mapper.getAllDailyInspectionDamageNoticeViewByDepartmentId(requestUtil.getInt("departmentId"));
                    requestUtil.responseWrite(Mapper.toJsonString(allDailyInspectionDamageNoticeView));
                }
            } else {
                List<DailyInspectionDamageNoticeView> allDailyInspectionDamageNoticeView = mapper.getAllDailyInspectionDamageNoticeView();
                requestUtil.responseWrite(Mapper.toJsonString(allDailyInspectionDamageNoticeView));
            }
        } catch (Exception e) {
            log.error(e.toString());
            requestUtil.responseFail("未知错误");
        } finally {
            viewMapperMapper.closeSession();
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) {

    }
}
